﻿using Newtonsoft.Json;
using Stee.CAP8.MessageBus.Interface.Models;
using Stee.CAP8.MessageBus.Sagas.Core.Common;
using Stee.CAP8.MessageBus.Sagas.Core.Sagas.SagaData;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Stee.CAP8.MessageBus.Sagas.Core.Sagas
{

    public interface ISaga
    {
    }

    public abstract class BaseMainSaga<SagaData> : ISaga where SagaData: BaseSagaData
    {
        public IBus _bus;

        public  ISagaStorage _sagaStorage;
        SagaData _sagaData;

        protected SagaData Data => _sagaData;


        private CommandMessage _commandMessage;
        public void SendCommand()
        {
            _bus.SendCommand(_commandMessage);
        }
        public void SendCommand(string actionName, string topicName, string replyChannel, object instance)
        {
            _commandMessage.Action = actionName;
            _commandMessage.Source = topicName;
            _commandMessage.ReplyChannel = replyChannel;
            _commandMessage.Data = instance.ObjToBytes();
            SendCommand();
        }
        public abstract Task HandleReply(CommandMessage message,SagaData sagaData);

       
        
        public void SetSagaData(SagaData sagaData) 
        {
            _sagaData = sagaData;
        }


        public void SetCommandMessage(CommandMessage message) 
        {
            _commandMessage = message;
        }
    }
    
}
